<template>
  <div class="admin-dashboard">
    <el-container style="height: 100vh;">
      <!-- 侧边栏 -->
      <el-aside width="200px" class="sidebar">
        <div class="logo">
          <h2>{{ t('admin.title') }}</h2>
        </div>
        <el-menu
          :default-active="$route.path"
          class="sidebar-menu"
          :router="true"
        >
          <el-menu-item index="/admin/dashboard">
            <el-icon><House /></el-icon>
            <span>{{ t('admin.menu.dashboard') }}</span>
          </el-menu-item>
          <el-menu-item index="/admin/merchants">
            <el-icon><Shop /></el-icon>
            <span>{{ t('admin.menu.merchants') }}</span>
          </el-menu-item>
          <el-menu-item index="/admin/products">
            <el-icon><Goods /></el-icon>
            <span>{{ t('admin.menu.products') }}</span>
          </el-menu-item>
          <el-menu-item index="/admin/exchange-codes">
            <el-icon><Ticket /></el-icon>
            <span>{{ t('admin.menu.exchangeCodes') }}</span>
          </el-menu-item>
          <el-menu-item index="/admin/orders">
            <el-icon><Document /></el-icon>
            <span>{{ t('admin.menu.orders') }}</span>
          </el-menu-item>
          <el-menu-item index="/admin/analytics">
            <el-icon><DataAnalysis /></el-icon>
            <span>{{ t('admin.menu.analytics') }}</span>
          </el-menu-item>
          <el-menu-item index="/admin/settings">
            <el-icon><Setting /></el-icon>
            <span>{{ t('admin.menu.settings') }}</span>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <!-- 主内容区域 -->
      <el-container>
        <!-- 头部 -->
        <el-header class="header">
          <div class="header-content">
            <h1>{{ currentPageTitle }}</h1>
            <div class="user-actions">
              <el-dropdown @command="handleUserAction">
                <span class="user-info">
                  <el-avatar :size="32" icon="User" />
                  <span class="username">{{ user?.username }}</span>
                  <el-icon><ArrowDown /></el-icon>
                </span>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item command="profile">{{ t('admin.profile') }}</el-dropdown-item>
                    <el-dropdown-item command="logout">{{ t('admin.logout') }}</el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </div>
          </div>
        </el-header>

        <!-- 页面内容 -->
        <el-main class="main-content">
          <router-view />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import { computed } from 'vue'
import { useI18n } from 'vue-i18n'
import { useStore } from 'vuex'
import { useRouter, useRoute } from 'vue-router'
import { 
  House, 
  Shop, 
  Goods, 
  Ticket, 
  Document, 
  DataAnalysis, 
  Setting, 
  ArrowDown 
} from '@element-plus/icons-vue'

export default {
  name: 'AdminLayout',
  components: {
    House,
    Shop,
    Goods,
    Ticket,
    Document,
    DataAnalysis,
    Setting,
    ArrowDown
  },
  setup() {
    const { t } = useI18n()
    const store = useStore()
    const router = useRouter()
    const route = useRoute()
    
    const user = computed(() => store.state.user)
    
    const menuTitles = {
      '/admin/dashboard': t('admin.menu.dashboard'),
      '/admin/merchants': t('admin.menu.merchants'),
      '/admin/products': t('admin.menu.products'),
      '/admin/exchange-codes': t('admin.menu.exchangeCodes'),
      '/admin/orders': t('admin.menu.orders'),
      '/admin/analytics': t('admin.menu.analytics'),
      '/admin/settings': t('admin.menu.settings')
    }
    
    const currentPageTitle = computed(() => {
      return menuTitles[route.path] || t('admin.menu.dashboard')
    })
    
    const handleUserAction = (command) => {
      if (command === 'logout') {
        store.dispatch('logout')
        router.push('/login')
      } else if (command === 'profile') {
        // Navigate to profile page
        console.log('Navigate to profile')
      }
    }
    
    return {
      t,
      user,
      currentPageTitle,
      handleUserAction
    }
  }
}
</script>

<style scoped>
.admin-dashboard {
  height: 100vh;
}

.sidebar {
  background-color: #304156;
  color: #bfcbd9;
  overflow-y: auto;
}

.logo {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #2d3d4d;
  color: white;
}

.sidebar-menu {
  border-right: none;
  background-color: #304156;
}

.sidebar-menu :deep(.el-menu-item) {
  color: #bfcbd9;
}

.sidebar-menu :deep(.el-menu-item:hover) {
  background-color: #2d3d4d;
  color: #409eff;
}

.sidebar-menu :deep(.el-menu-item.is-active) {
  background-color: #1f2d3d;
  color: #409eff;
}

.header {
  background-color: #ffffff;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  padding: 0;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
  padding: 0 20px;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.username {
  margin: 0 10px;
  font-weight: 500;
}

.main-content {
  background-color: #f0f2f5;
  padding: 20px;
}

:deep(.el-main) {
  padding: 20px !important;
}
</style>